import { _decorator, Component, JsonAsset, Node, Prefab, resources, SpriteFrame } from 'cc';
const { ccclass, property,executionOrder } = _decorator;

@ccclass('ResMgr')
@executionOrder(-1)
export class ResMgr extends Component {

    private static inst:ResMgr|null=null;

   
    public static get Inst(){
        return ResMgr.inst;
    }

    protected onLoad(): void {
        if(ResMgr.inst==null){
            ResMgr.inst=this;
        }
    }

    protected start(): void {
//        this.test();
    }

    test(){


        this.loadJson("Animal").then((d)=>{
        console.log(d);
         });
    }

    loadPrefab(path:string):Promise<any> {
        let path2="prefab/"+path;
        return new Promise((res,rej)=>{
            resources.load(path2,Prefab,(e,data)=>{
                if(e){
                    console.info("err load "+path2);
                                    res(null);
                return;
                }else{
                    res(data);
                }

            })
        })
    }

    loadSp(path:string):Promise<any> {
        let path2="sprite/"+path;

        return new Promise((res,rej)=>{
            resources.load(path2,SpriteFrame,(e,data)=>{
                if(e){
                    res(null);
                    return;
                }else{
                    res(data);
                }
            })
        })
    }

    loadJson(path:string):Promise<any>{

        let path2 ="json/"+path;
        return new Promise((res,rej)=>{
            resources.load(path2,JsonAsset,(e,data)=>{
                if(e){
                    console.info("err load "+path2);
                res(null);
                return;
                }else{
                    res(data.json);
                }
            })
        })
    }

}


